<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\admin\logic;

use app\model\Goods;
use think\facade\Request;

class GoodsLogic
{

    public function getList()
    {
        $param = Request::get();

        $where = [];
        if(isset($param['name']) || !empty($param['name'])){
            $where[] = ['goods_name','like','%'.trim($param['name']).'%'];
        }

        $where[] = ['goods_status','=',1];    //默认是 没删除的
        $where[] = ['goods_orgid','=',config('user')['orgid']];
        $goodsModel = new Goods();
        $list = $goodsModel->getGoodsListByCondition($where);
        return $list;
    }

    public function addGoods()
    {
        $param = Request::post();

        $goods = [
            'goods_orgid' => config('user.orgid')
        ];
        if(!isset($param['name']) || empty($param['name'])) fail('请输入商品名称');
        $goods['goods_name'] = trim($param['name']);

        if(!isset($param['stock']) || empty($param['stock'])) fail('请输入商品库存');
        $goods['goods_stock'] = intval($param['stock']);

        if(!isset($param['exchangeIntegral']) || empty($param['exchangeIntegral'])) fail('请输入兑换对应的积分');
        $goods['goods_exchange_integral'] = intval($param['exchangeIntegral']);

        if(!isset($param['pic']) || empty($param['pic'])) fail('请上传封面图');
        $goods['goods_pic'] = fileFullPath(trim($param['pic']));

        $goodsModel = new Goods();
        $rt = $goodsModel->addOneGoods($goods);
        if(!$rt)    fail('商品添加失败');

        return $rt;
    }

    public function updateGoods()
    {
        $param = Request::post();

        if(!isset($param['goodsId']) || empty($param['goodsId'])) fail('请选择商品');
        $goodsId= intval($param['goodsId']);
        $goodsModel = new Goods();
        $info = $goodsModel->getGoodsInfoById($goodsId);
        if(!$info)   fail('商品信息不存在');

        $goods = [
            'goods_update_uid' => config('user')['id'],
            'goods_update_name' => config('user')['pername']
        ];
        if(!isset($param['name']) || empty($param['name'])) fail('请输入商品名称');
        $goods['goods_name'] = trim($param['name']);

        if(!isset($param['stock']) || empty($param['stock'])) fail('请输入商品库存');
        $goods['goods_stock'] = intval($param['stock']);

        if(!isset($param['exchangeIntegral']) || empty($param['exchangeIntegral'])) fail('请输入兑换对应的积分');
        $goods['goods_exchange_integral'] = intval($param['exchangeIntegral']);

        if(!isset($param['pic']) || empty($param['pic'])) fail('请上传封面图');
        $goods['goods_pic'] = fileFullPath(trim($param['pic']));

        $rt = $goodsModel->updateGoodsInfoByWhere(['goods_id'=>$goodsId],$goods);
        if(!$rt)    fail('商品修改失败');

        return $rt;
    }

    public function del($goodsId){

        $goodsModel = new Goods();
        $goodsInfo = $goodsModel->getGoodsInfoById($goodsId);

        if(!$goodsInfo) fail('商品不存在');

        if($goodsInfo['status'] == 0)   fail('商品已经删除');

        $goods = [
            'goods_status' => 0,
            'goods_update_uid' => config('user.id'),
            'goods_update_name' => config('user.pername'),
        ];
        $rt = $goodsModel->updateGoodsInfoByWhere(['goods_id'=>$goodsId],$goods);
        if(!$rt)    fail('商品删除失败');

        return $rt;
    }

}